import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/regist/ptxy/viewModel/ptxy_viewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:webview_flutter/webview_flutter.dart';

class PTXYView extends BaseStatefulWidget {
  final String type; //协议类型 1:平台协议 2:自雇者协议

  PTXYView({this.type});
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return _PTXYViewState();
  }
}

class _PTXYViewState extends BaseState<PTXYViewModel, PTXYView> {
  bool _isLoading = true; //是否 在加载
  String _urlString; // 加载的url
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget

    return Scaffold(
      appBar: MTopBar(title: '协议').create(),
      body: _isLoading ? LoadingWidget() : _bodyView(),
    );
  }

  _bodyView() {
    return WebView(
      initialUrl: _urlString,
      onPageFinished: (String url) {
        // print('页面加载完成' + url);
        // Navigator.pop(context);
      },
    );
  }

  _getData() async {
    String type = widget.type;
    viewModel.getAppAgreement(type: type);
    viewModel.getAppAgreementStream().listen((event) {
      print(event);
      if (event['code'] == 0) {
        setState(() {
          _isLoading = false;
          _urlString = event['data'] ?? '';
        });
      } else {
        ToastUtils.showToast(event['msg'] ?? '系统异常');
      }
    });
  }
}
